﻿using System;
using Dragon.Library.Framework.Interface.Bo;
using Dragon.Library.Framework.Interface.Dao;

namespace Dragon.Library.Framework.Bo
{
    public abstract partial class BaseBo<T, TBean, TKey> : IBaseBo
        where T : IBaseDao<TBean, TKey>, new()
        where TBean : class,ICloneable, new()
    {
        private bool _disposed;

        protected T Dao;

        protected BaseBo()
        {
            _disposed = false;
            Dao = new T();
        }

        public void InitializationSession()
        {
            if (!Dao.IsTran)
                Dao.Initialization(Guid.NewGuid().ToString());
        }

        public void InitializationDao()
        {
            if (!Dao.IsOpen)
                Dao.ConnectionOpen();
        }

        public void DisposeDao()
        {
            if (Dao.IsOpen && !Dao.IsTran)
                Dao.ConnectionClose();
        }

        public void DisposeSession()
        {
            if (!Dao.IsTran)
                Dao.Clear();
        }

        #region [ IDisposable ]

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {

                }
                Dao.Dispose();
            }
            _disposed = true;
        }

        #endregion

        ~BaseBo()
        {
            Dispose(false);
        }
    }
}
